package com.wanhe.qrcode;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.google.zxing.ResultPoint;

import java.util.ArrayList;
import java.util.List;

/**
 * 扫描框界面
 * 
 * @author Administrator
 * 
 */
@SuppressLint("DrawAllocation")
public final class ViewfinderView extends View {

	private final long ANIMATION_DELAY = 10L;
	private final int MAX_RESULT_POINTS = 20;

	private CameraManager cameraManager;
	private final Paint paint;
	private final int maskColor;
	private List<ResultPoint> possibleResultPoints;

	private Bitmap bmp_scanner_animation;
	private boolean isFirst;
	/**
	 * 中间滑动线的最顶端位置
	 */
	private int slideTop;

	/**
	 * 中间那条线每次刷新移动的距离
	 */
	private final int SPEEN_DISTANCE = 5;
	/**
	 * 字体大小
	 */
	private final int TEXT_SIZE = 15;
	private final String tipText;
	private float textOffset;
	/**
	 * 手机的屏幕密度
	 */
	private static float density;
	/**
	 * 四个绿色边角对应的长度
	 */
	private int ScreenRate;
	/**
	 * 四个绿色边角对应的宽度
	 */
	private static final int CORNER_WIDTH = 4;
	public static final int CORNER_WIDTH_DOUBLE = 12;
	private final int color_scanner_box;

	// This constructor is used when the class is built from an XML resource.
	public ViewfinderView(Context context, AttributeSet attrs) {
		super(context, attrs);

		density = context.getResources().getDisplayMetrics().density;
		// 将像素转换成dp
		ScreenRate = (int) (30 * density);

		// Initialize these once for performance rather than calling them every
		// time in onDraw().
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		Resources resources = getResources();
		maskColor = resources.getColor(R.color.viewfinder_mask);
		color_scanner_box = resources.getColor(R.color.qr_scanner_box);
		tipText = resources.getString(R.string.addfriend_txt_QRcode_intro);

		possibleResultPoints = new ArrayList<ResultPoint>(5);
		bmp_scanner_animation = BitmapFactory.decodeResource(resources, R.drawable.qr_scanner_animation);

	}

	public void setCameraManager(CameraManager cameraManager) {
		this.cameraManager = cameraManager;
	}

	@Override
	public void onDraw(Canvas canvas) {
		Rect frame = cameraManager.getFramingRect();

		if (frame == null) {
			return;
		}
		frame = new Rect(frame.top, frame.left, frame.bottom, frame.right);

		int width = canvas.getWidth();
		int height = canvas.getHeight();

		paint.setColor(maskColor);
		canvas.drawRect(0, 0, width, frame.top, paint);
		canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
		canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
		canvas.drawRect(0, frame.bottom + 1, width, height, paint);

		// 画四个角
		paint.setColor(color_scanner_box);

		canvas.drawRect(frame.left + CORNER_WIDTH, frame.top, frame.left + ScreenRate,
				frame.top + CORNER_WIDTH, paint);
		canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH,
				frame.top + ScreenRate, paint);

		canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right - CORNER_WIDTH,
				frame.top + CORNER_WIDTH, paint);
		canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right,
				frame.top + ScreenRate, paint);

		canvas.drawRect(frame.left, frame.bottom - ScreenRate,
				frame.left + CORNER_WIDTH, frame.bottom - CORNER_WIDTH, paint);
		canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left
				+ ScreenRate, frame.bottom, paint);

		canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate,
				frame.right, frame.bottom, paint);
		canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH,
				frame.right, frame.bottom, paint);

		// 画扫描框下面的字
		paint.setColor(Color.WHITE);
		paint.setTextSize(TEXT_SIZE * density);
		paint.setAlpha(0x40);
		paint.setTypeface(Typeface.create("System", Typeface.BOLD));

		// 初始化中间线滑动的最上边和最下边
		if (!isFirst) {
			isFirst = true;
			slideTop = frame.top;
			canvas.drawText(tipText, frame.left, frame.bottom + 50
					* density, paint);
			float w = paint.measureText(tipText);
			textOffset = (width - w) / 2;

		} else {
			Rect pic = new Rect(frame.left, slideTop, frame.right, slideTop + 3);
			canvas.drawBitmap(bmp_scanner_animation, null, pic, paint);

			canvas.drawText(tipText, textOffset, frame.bottom + 50
					* density, paint);
		}
		// 绘制中间的线,每次刷新界面，中间的线往下移动SPEEN_DISTANCE
		slideTop += SPEEN_DISTANCE;
		if (slideTop >= frame.bottom) {
			slideTop = frame.top;
		}

		// 只刷新扫描框的内容，其他地方不刷新
		postInvalidateDelayed(ANIMATION_DELAY, frame.left - CORNER_WIDTH, frame.top - CORNER_WIDTH, frame.right
				+ CORNER_WIDTH, frame.bottom + CORNER_WIDTH);
	}

	public void drawViewfinder() {
		invalidate();
	}

	public void addPossibleResultPoint(ResultPoint point) {
		List<ResultPoint> points = possibleResultPoints;
		synchronized (points) {
			points.add(point);
			int size = points.size();
			if (size > MAX_RESULT_POINTS) {
				// trim it
				points.subList(0, size - MAX_RESULT_POINTS / 2).clear();
			}
		}
	}

}
